#include "MutaRepeat.h"


MutaRepeat::MutaRepeat(QString owner,qreal xP,qreal y,int layer)
    : MutaObject(owner.append(QString().number(MutaObject::getObjectCount())),Muta::Repeat,owner),
      QGraphicsLineItem(0,0,0,2000),
      layer(layer)
{
    setPos(QPointF(xP,0));
    setCacheMode( QGraphicsItem::ItemCoordinateCache );
    setFlag( QGraphicsItem::ItemIsSelectable, true );
    setFlag( QGraphicsItem::ItemIsMovable, true );
    setFlag(QGraphicsItem::ItemSendsGeometryChanges);
    setColor();
}

int MutaRepeat::type() const
{
    return mutaType;
}

void MutaRepeat::setPos(const QPointF &pos)
{
    QGraphicsLineItem::setPos(Muta::quantizeD(pos.x(),Muta::BEAT_PIXELS),0);
    //setColor();
    emit posChanged(objectID,pos);
}

void MutaRepeat::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
{
    QGraphicsLineItem::mouseMoveEvent(event);
    setPos(this->pos());
    //setColor();
    //emit posChanged(objectID,event->scenePos());
}

int MutaRepeat::getLayer()
{
    return layer;
}

void MutaRepeat::setColor()
{
        QColor fillColor;
        QColor outlineColor;
        switch(layer) {
            case 0:
                fillColor = QColor(255,255,255,255);
                outlineColor = QColor(255,255,255,255);
                break;
            case 1:
                fillColor = QColor(0,155,220,255);
                outlineColor = QColor(255,255,255,255);
                break;
            case 2:
                fillColor = QColor(50,255,50,255);
                outlineColor = QColor(255,255,255,255);
                break;
            case 3:
                fillColor = QColor(255,50,50,255);
                outlineColor = QColor(255,255,255,255);
                break;
            case 4:
                fillColor = QColor(0,0,0,255);
                outlineColor = QColor(255,255,255,255);
                break;
        }



    QBrush brush;
    brush.setColor(fillColor);
    brush.setStyle(Qt::SolidPattern);

    QPen pen;
    //this->setBrush(brush);
    pen.setWidth(5);
    pen.setColor( fillColor );
    pen.setJoinStyle(Qt::RoundJoin);
    pen.setCapStyle(Qt::RoundCap);
    setPen( pen );
}

std::ostream& operator<<(std::ostream &out, MutaRepeat &repeat)
{
    out << "MutaRepeat " << repeat.getObjectID().toAscii().data() << " " << (float)repeat.x();
    return out;
}

QVariant MutaRepeat::itemChange(GraphicsItemChange change, const QVariant &value) {
    QVariant variant = QGraphicsLineItem::itemChange(change, value);

    if(change == QGraphicsItem::ItemPositionChange || change == QGraphicsItem::ItemPositionHasChanged)
    {
        variant.setValue(QPointF(Muta::quantizeD(variant.toPointF().x(),Muta::BEAT_PIXELS),0));
    }

    return variant;
}

QStringList MutaRepeat::getProperties()
{
    QStringList properties;

    properties << "rt" << QString::number(x());
    return properties;

}
